package com.rpframework.website.device.web.directive;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.rpframework.core.freemarker.directive.BaseTemplateDirectiveModel;
import com.rpframework.core.freemarker.directive.DirectiveUtils;
import com.rpframework.core.utils.DictionarySettingUtils;
import com.rpframework.core.utils.SpringUtils;
import com.rpframework.website.device.domain.Estate;
import com.rpframework.website.device.domain.Goods;
import com.rpframework.website.device.service.EstateService;
import com.rpframework.website.device.service.GoodsService;

import freemarker.core.Environment;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component("device")
public class YLDirectiveModel extends BaseTemplateDirectiveModel {

	final Logger logger = LoggerFactory.getLogger(getClass());
	static final String GET_VILLAGE_LIST = "get_village_list";
	static final String GET_ROOM_LIST = "get_room_list";
	static final String GET_HOBBIES_LIST = "get_hobbies_list";
	static final String GET_ESTATE_LIST = "get_estate_list";
	static final String GET_GOODS_CATEGORY = "get_goods_category";
	static final String GET_GOODS_LABEL = "get_goods_label";
	static final String GET_GOODS_UNIT = "get_goods_unit";
	static final String GET_GOODS_LIST = "get_goods_list";

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {

		String cmd = DirectiveUtils.getString("cmd", params);
		Map paramWarp = new HashMap(params);
		boolean pass = true;
		if (StringUtils.isBlank(cmd)) {

		} 
		else if (StringUtils.equals(cmd, GET_GOODS_LIST)) {
			GoodsService goodsService = SpringUtils.getBean(GoodsService.class);
			List<Goods> goodsList = goodsService.queryAll();
			paramWarp.put("goodsList", ObjectWrapper.DEFAULT_WRAPPER.wrap(goodsList));
		}
		else if (StringUtils.equals(cmd, GET_ESTATE_LIST)) {
			EstateService estateService = SpringUtils.getBean(EstateService.class);
			List<Estate> estateList = estateService.queryAll();
			paramWarp.put("estateList", ObjectWrapper.DEFAULT_WRAPPER.wrap(estateList));
		}
		else if (StringUtils.equals(cmd, GET_GOODS_CATEGORY)) {
			Map map = DictionarySettingUtils.getParameterMap("goods.category");//获取数据字典里 该类型的所有值
			paramWarp.put("goodsCategoryList", ObjectWrapper.DEFAULT_WRAPPER.wrap(map));
		}
		else if (StringUtils.equals(cmd, GET_GOODS_LABEL)) {
			Map map = DictionarySettingUtils.getParameterMap("goods.label");//获取数据字典里 该类型的所有值
			paramWarp.put("goodsLabelList", ObjectWrapper.DEFAULT_WRAPPER.wrap(map));
		}
		else if (StringUtils.equals(cmd, GET_GOODS_UNIT)) {
			Map map = DictionarySettingUtils.getParameterMap("goods.unit");//获取数据字典里 该类型的所有值
			paramWarp.put("goodsUnitList", ObjectWrapper.DEFAULT_WRAPPER.wrap(map));
		}
		Map origWarp = DirectiveUtils.addParamsToVariable(env, paramWarp);
		if (pass && body != null) {
			body.render(env.getOut());
		}
		DirectiveUtils.removeParamsFromVariable(env, paramWarp, origWarp);
	}
}
